<?php

namespace app\console\controller;

use app\console\model\Baksql as Bakmysql;

use think\facade\Db;
use think\Request;
use think\facade\Config;
use think\facade\View;
use think\facade\Route;
/**
 * 数据库控制器------备份操作
 * Class Baksql
 * @package app\console\controller
 */
class Baksql extends Base
{
    //数据库备份
    public function index()
    {
        //$this->config['connections']['mysql']

        $sql = new Bakmysql(Config::get("database.connections.mysql"));

        echo View::fetch("index", [
            'mate_title' => '数据库备份',
            "list" => $sql->get_filelist()
        ]);
    }

    //数据库
    public function mysql(Request $request)
    {

        ini_set("memory_limit", "1024M"); // 不够继续加大
        set_time_limit(0);

        if ($request->param('table')) {

            $table = Db::query("SHOW FULL COLUMNS FROM " . $request->param('table'));

            View::assign('table', $table);

        } else {
            $data = Db::query("SHOW TABLE STATUS");
            $total = 0;
            foreach ($data as &$value) {
                $total += $value['Data_length'];
                $value['size'] = self::GetRealSize($value['Data_length']);
            }
            View::assign('total', self::GetRealSize($total));
            View::assign('tables', $data);
        }

        return View::fetch('mysql', [
            'mate_title' => "数据库备份"
        ]);
    }

    private function GetRealSize($size)
    {
        $kb = 1024;          // Kilobyte
        $mb = 1024 * $kb;    // Megabyte
        $gb = 1024 * $mb;    // Gigabyte
        $tb = 1024 * $gb;    // Terabyte

        if ($size < $kb)
            return $size . 'B';

        else if ($size < $mb)
            return round($size / $kb, 2) . 'KB';

        else if ($size < $gb)
            return round($size / $mb, 2) . 'MB';

        else if ($size < $tb)
            return round($size / $gb, 2) . 'GB';

        else
            return round($size / $tb, 2) . 'TB';
    }

    /**
     * 备份
     * @param Request $request
     * @return bool
     */
    public function backup(Request $request)
    {
        $sql = new Bakmysql(Config::get("database.connections.mysql"));
        $result = $sql->backup();

       // $this->_initialize();
        if ($result) {
            win($result,Route::buildUrl('index'));
        } else {
            fail($sql->getError());
        }

    }

    /**
     * 下载
     * @param Request $request
     * @return bool
     */
    public function download(Request $request)
    {
        $sql = new Bakmysql(Config::get("database.connections.mysql"));

        return $sql->downloadFile($request->param('name'));
    }

    /**
     * 还原
     * @param Request $request
     * @return bool
     */
    public function restore_data(Request $request)
    {
        $sql = new Bakmysql(Config::get("database.connections.mysql"));

        $result = $sql->restore($request->param('name'));

        if ($result) {
            win($result,Route::buildUrl('index'));
        } else {
            fail($sql->getError());
        }

    }

    /**
     * 删除
     * @param Request $request
     * @return bool
     */
    public function delete(Request $request)
    {
        $sql = new Bakmysql(Config::get("database.connections.mysql"));

        $result = $sql->delfilename($request->param('name'));

        if ($result) {
            win($result,Route::buildUrl('index'));
        } else {
           fail($sql->getError());
        }
    }
}